import {Command} from '../constant';
import Taro from '@tarojs/taro';
import {Dispatch} from 'typings';
import {IAllReducerProps} from '../types';
import {getReducerData} from '@/redux/store';
import {extraPathsValue} from '@/redux/util';
import api from 'api';
import {ant} from 'wmkit';

export default (dispatch: Dispatch) => {
  let action = {
    commonChange(...param: any) {
      dispatch({
        type: Command.commonChange,
        payload: extraPathsValue(...arguments),
      });
    },
    async page(memberType: number | string) {
      const {
        main: {isAquamarineMember, isCrystalMember},
      } = getData();
      // let memberType = null;
      // if (isAquamarineMember && isCrystalMember) {
      //   //双会员
      //   memberType = 2;
      // } else if (isAquamarineMember && !isCrystalMember) {
      //   //蓝晶会员
      //   memberType = 0;
      // } else if (!isAquamarineMember && isCrystalMember) {
      //   //赤晶会员
      //   memberType = 1;
      // }
      let params = {
        enterWay: 0,
        memberType: memberType, //会员类型：0-蓝晶会员 1-赤晶会员 2双会员
      };
      const res = await api.OnecategoryController.findOnecategoryList(params); //查询权益类目-通道
      action.commonChange('main.list', res);
    },
    async toPage(item: any) {
      let {
        main: {customerId},
      } = getData();
      if (item.skipWay == 1 && item.url.startsWith('plugin')) {
        let query = {
          movieuid: customerId,
          movieapikey: await ant.getAntApiKey(),
          index: '/pages/index/index',
        };
        let kvarr = [];
        for (const key in query) {
          if (Object.prototype.hasOwnProperty.call(query, key)) {
            const e = query[key];
            kvarr.push(`${key}=${e}`);
          }
        }
        let url = `${item.url}?${kvarr.join('&')}`;
        Taro.navigateTo({
          url,
        });
      } else if (item.skipWay == 0 && item.url.startsWith('/pages/package-')) {
        Taro.navigateTo({
          url: item.url,
        });
      } else if (item.skipWay == 0 && item.url.startsWith('/pages')) {
        Taro.navigateTo({
          url: item.url,
        });
      }
    },
  };
  return action;
};

function getData(): IAllReducerProps {
  return {
    main: getReducerData('packageDServiceCentre'),
  };
}

//create by moon https://github.com/creasy2010/moon
